# 启动Agent Service及其依赖服务
Write-Host "启动Agent Service依赖..." -ForegroundColor Green

# 检查Redis
Write-Host "`n检查Redis..." -ForegroundColor Yellow
$redisRunning = Test-NetConnection -ComputerName localhost -Port 6379 -WarningAction SilentlyContinue -InformationLevel Quiet -ErrorAction SilentlyContinue
if (-not $redisRunning) {
    Write-Host "尝试启动Redis (Docker)..." -ForegroundColor Yellow
    docker start redis 2>$null
    if ($LASTEXITCODE -ne 0) {
        Write-Host "Redis未运行，请先启动Redis" -ForegroundColor Red
        Write-Host "启动方式: docker run -d -p 6379:6379 --name redis redis:latest" -ForegroundColor Yellow
        $continue = Read-Host "是否继续？(y/n)"
        if ($continue -ne "y") {
            exit 1
        }
    } else {
        Write-Host "✓ Redis已启动" -ForegroundColor Green
        Start-Sleep -Seconds 2
    }
} else {
    Write-Host "✓ Redis运行中" -ForegroundColor Green
}

# 检查并启动LLM Service
Write-Host "`n检查LLM Service (端口 8084)..." -ForegroundColor Yellow
$llmRunning = Test-NetConnection -ComputerName localhost -Port 8084 -WarningAction SilentlyContinue -InformationLevel Quiet -ErrorAction SilentlyContinue
if (-not $llmRunning) {
    Write-Host "启动LLM Service..." -ForegroundColor Yellow
    Start-Process powershell -ArgumentList "-NoExit", "-Command", "cd '$PWD\llm-service'; mvn spring-boot:run" -WindowStyle Normal
    Write-Host "等待LLM Service启动..." -ForegroundColor Gray
    Start-Sleep -Seconds 10
} else {
    Write-Host "✓ LLM Service运行中" -ForegroundColor Green
}

# 检查并启动Code Analysis Service
Write-Host "`n检查Code Analysis Service (端口 8083)..." -ForegroundColor Yellow
$analysisRunning = Test-NetConnection -ComputerName localhost -Port 8083 -WarningAction SilentlyContinue -InformationLevel Quiet -ErrorAction SilentlyContinue
if (-not $analysisRunning) {
    Write-Host "启动Code Analysis Service..." -ForegroundColor Yellow
    Start-Process powershell -ArgumentList "-NoExit", "-Command", "cd '$PWD\code-analysis-service'; mvn spring-boot:run" -WindowStyle Normal
    Write-Host "等待Code Analysis Service启动..." -ForegroundColor Gray
    Start-Sleep -Seconds 10
} else {
    Write-Host "✓ Code Analysis Service运行中" -ForegroundColor Green
}

# 检查并启动Code Generation Service
Write-Host "`n检查Code Generation Service (端口 8082)..." -ForegroundColor Yellow
$generationRunning = Test-NetConnection -ComputerName localhost -Port 8082 -WarningAction SilentlyContinue -InformationLevel Quiet -ErrorAction SilentlyContinue
if (-not $generationRunning) {
    Write-Host "启动Code Generation Service..." -ForegroundColor Yellow
    Start-Process powershell -ArgumentList "-NoExit", "-Command", "cd '$PWD\code-generation-service'; mvn spring-boot:run" -WindowStyle Normal
    Write-Host "等待Code Generation Service启动..." -ForegroundColor Gray
    Start-Sleep -Seconds 10
} else {
    Write-Host "✓ Code Generation Service运行中" -ForegroundColor Green
}

# 等待所有服务就绪
Write-Host "`n等待所有服务就绪..." -ForegroundColor Yellow
Start-Sleep -Seconds 5

# 启动Agent Service
Write-Host "`n启动Agent Service..." -ForegroundColor Green
Write-Host "Agent Service将在当前窗口运行" -ForegroundColor Gray
Write-Host "按 Ctrl+C 停止服务" -ForegroundColor Gray
Write-Host "`n" -NoNewline

cd agent-service
mvn spring-boot:run

